package com.pro_one.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pro_one.dao.MemberDao;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.pojo.Member;
import com.pro_one.service.MemberService;
import com.pro_one.util.MD5Utils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    /**
     * 根据电话查询会员信息
     *
     * @param telephone
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    /**
     * 添加会员
     *
     * @param member
     */
    @Override
    public void add(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    /**
     * 获取会员信息用于页面展示折线图
     *
     * @return
     */
    @Override
    public Map getMemberReport() {
        List<String> months = new ArrayList<>();
        List<Integer> membersCount = new ArrayList<>();

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            months.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));
        }
        for (String month : months) {
            //动态获取每月最后一天
            String[] strings = month.split("\\.");
            calendar.set(Integer.parseInt(strings[0]), Integer.parseInt(strings[1]) - 1, 1);
            calendar.roll(Calendar.DATE, -1);
            String lastDay = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
            //查询每月会员数
            int count = memberDao.getMembersCountByMonths(lastDay);
            membersCount.add(count);
        }
        Map map = new HashMap();
        map.put("months", months);
        map.put("memberCount", membersCount);
        return map;
    }

    //会员分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询 基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);
Page<Member> page = memberDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Member> rows = page.getResult();
        return new PageResult(total,rows);
    }
    //根据会员id
    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }

    @Override
    public void deleteById(Integer id) {
        memberDao.deleteById(id);
    }

    @Override
    public List<Map<String,Object>> findByIds(String[] ids) {
        List<Map<String,Object>> list = new ArrayList<>();
        for (String id : ids) {
            Map<String, Object> map = new HashMap<>();
            Member member = memberDao.findById(Integer.parseInt(id));
            map.put("member",member);
            List<String> adderss = memberDao.findAddressByMemberId(Integer.parseInt(id));
            map.put("adderss",adderss);
            List<String> setlist = memberDao.findSetmealByMemberId(Integer.parseInt(id));
            String sname="";
            if(setlist.size()>0 && setlist!=null){
                for (int i = 0; i < setlist.size(); i++) {
                    String s1 = (String) setlist.get(i);
                    sname=sname+","+s1;
                }
            }
            map.put("sname",sname);

            List<String> checkgroups = memberDao.findCheckgroupByMemberId(Integer.parseInt(id));
            map.put("checkgroups",checkgroups);
            List<String> checkitems = memberDao.findCheckitemByMemberId(Integer.parseInt(id));
            map.put("checkitems",checkitems);
            for (Object checkitem : checkitems) {
                System.out.println(checkitem.toString());
            }

            list.add(map);
        }
        return list;
}
    @Override
    public List<Map<String,String>> findAllHealThmanagerName() {
        List<String> list = memberDao.findAllHealThmanagerName();
        List<Map<String,String>> mapList = new ArrayList<>();
        for (String s : list) {
            Map<String,String> map = new HashMap<>();
            map.put("value",s);
            mapList.add(map);
        }
        return mapList;
    }


}
